package com.aaa.system.aop;

import com.aaa.entity.OperLog;
import com.aaa.entity.User;
import com.aaa.system.aop.annotation.Log;
import com.aaa.system.mapper.UserMapper;
import com.aaa.system.service.OperLogService;
import com.aaa.utils.TokenUtils;
import com.aaa.utils.WebUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

@Component
@Aspect
public class OperLogAspect {

    @Autowired
    private OperLogService operLogService;
    @Autowired
    private UserMapper userMapper;

    //定义一个切点
    @Pointcut(value = "@annotation(com.aaa.system.aop.annotation.Log)")
    private void pointcut(){
    }
    //定义通知
    @AfterReturning(value = "pointcut()",returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result){
        //获取方法对象
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //获取使用Log注解的方法对象
        Method method = methodSignature.getMethod();
        //获取该方法的注解
        Log log = method.getAnnotation(Log.class);
        //获取title
        String title = log.title();
        //获取业务businessType
        String businessType = log.business_type();
        //获取方法名称
        String methodName = method.getName();
        //获取请求的类型
        HttpServletRequest request = WebUtil.getRequest();
        String requestMethod = request.getMethod();
        //获取操作人的名字opername
        String token = request.getHeader("token");
        String phone = TokenUtils.getInfo(token).get("username").toString();
        //根据手机号进行查询
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",phone);
        queryWrapper.eq("status",0);
        queryWrapper.eq("del_flag",0);
        String userName = userMapper.selectOne(queryWrapper).getUserName();

        //获取操作路径
        String oper_url = request.getServletPath();
        //操作oper_ip
        String oper_ip = request.getRemoteAddr();
        //操作地点oper_location
        String oper_location = oper_ip.startsWith("192.168.") ? "内网IP" : "外网IP";
        //请求操作的参数oper_param
        Object[] args = joinPoint.getArgs();
        String oper_param = JSON.toJSONString(args);
        //返回结果
        String json_result = JSON.toJSONString(result);
        String status= "0";
        LocalDateTime oper_time= LocalDateTime.now();
        OperLog operLog = new OperLog(null,title,businessType,methodName,requestMethod,log.operator_type(),userName,oper_url,oper_ip,oper_location,oper_param,json_result,status,null,oper_time);

        operLogService.OperLoginsert(operLog);

    }
}
